package cn.edu.ctbu.myfirstweb.service.impl;

import cn.edu.ctbu.myfirstweb.dao.CourseRepository;
import cn.edu.ctbu.myfirstweb.dao.TeacherRepository;
import cn.edu.ctbu.myfirstweb.domain.Course;
import cn.edu.ctbu.myfirstweb.domain.Teacher;
import cn.edu.ctbu.myfirstweb.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TeacherImpService implements TeacherService {
    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    CourseRepository courseRepository;

    public Page<Teacher> findAll(Example<Teacher> teacherExample, Pageable pageable) {
        return teacherRepository.findAll(teacherExample, pageable);
    }

    public Page<Teacher> findAll(Pageable pageable) {
        return teacherRepository.findAll(pageable);
    }

    public void deleteByTid(long tid) {
        teacherRepository.deleteByTid(tid);
    }

    public List<Teacher> findAll() {
        return teacherRepository.findAll();
    }

    public Teacher insert(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    public Teacher update(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    public Teacher query(Long id) {
        Optional<Teacher> teacher = teacherRepository.findById(id);
        return teacher.orElseThrow(() -> new RuntimeException("No such teacher"));
    }

    public Teacher queryByTidAndPassword(Long tid, String password) {
        return teacherRepository.findByTidAndAndPassword(tid, password);
    }

    public List<Course> getCourseByTid(Long Tid) {
        return courseRepository.getCourseByTid(Tid);
    }
}
